from datetime import datetime

from applications.extensions import db

class VoiceDetail(db.Model):
    __tablename__ = 'voice_detail'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    voice_url = db.Column(db.String(255), nullable=True, comment='链接')
    voice_size = db.Column(db.Float, nullable=True, comment='大小')
    voice_length = db.Column(db.Integer, nullable=True, comment='长度')
    post_id = db.Column(db.Integer,db.ForeignKey('post.id'), nullable=True, comment='关联帖子的id')
    comment_id = db.Column(db.Integer, nullable=True, comment='关联的评论id')
    name = db.Column(db.String(255), nullable=True, comment='语音包内的语音条标题')
    package_id = db.Column(db.Integer, nullable=True, comment='关联的语音包id')
    update_time = db.Column(db.DateTime, nullable=True, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    create_time = db.Column(db.DateTime, nullable=True, default=datetime.utcnow, comment='创建时间')
    post = db.relationship('Post', backref=db.backref('voiceDetail', lazy=True))

    def to_dict(self):
        return {
            'id': self.id,
            'voiceUrl': self.voice_url,
            'voiceSize': self.voice_size,
            'voiceLength': self.voice_length,
            'postId': self.post_id,
            'commentId':self.comment_id,
            'name':self.name,
            'packageId':self.package_id,
            'updateTime': self.update_time.isoformat() if self.update_time else None,
            'createTime': self.create_time.isoformat() if self.create_time else None
        }